package com.hd.openapi.servies.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hd.openapi.common.ResponseResult;
import com.hd.openapi.common.ResultTable;
import com.hd.openapi.dao.MenuMapper;
import com.hd.openapi.dao.RoleMapper;
import com.hd.openapi.entity.Menu;
import com.hd.openapi.entity.Role;
import com.hd.openapi.servies.pageroleser;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class pageroleserIMPL implements pageroleser {
    @Autowired
    private RoleMapper rm;
    @Autowired
    private MenuMapper a;

    @Override
    public ResultTable getroledata(Integer page, Integer limit, String name) {
        PageHelper.startPage (page,limit);
        List<Role> a= rm.getrolealldata(name);
        PageInfo pageInfo=new PageInfo (a);

        return new ResultTable (0,"",(int) pageInfo.getTotal (),pageInfo.getList ());
    }

    @Override
    public ResponseResult updaterolestatus(Integer id, Integer status) {
        if (id>0) {
            int i = rm.updateByPrimaryKeySelective (new Role (id, status));
            if (i>0) {
                return ResponseResult.getSuccess ("账户修改成功");
            }
        }
        return ResponseResult.getSuccess ("账户修改失败");
    }

    @Override
    public ResponseResult adddata(Role role) {
        int i=rm.adddata(role);
        if (i>0) {
            return ResponseResult.getSuccess ();
        }
        return ResponseResult.getFail ("新增失败");
    }

    @Override
    public ResponseResult updatadata(Role role) {
        int i = rm.updateByPrimaryKeySelective(role);
        if (i>0) {
            return ResponseResult.getSuccess ();
        }
        return ResponseResult.getFail ("修改失败");
    }

    @Override
    public List<Integer> getrolemenudata(Integer roleId) {
            List<Integer> aa= rm.getrolemenualldata(roleId);
        return aa;
    }

    @Override
    public List<Menu> getmenualldata() {
        List<Menu> aa= a.getmenualldata();
        List<Menu> fu = new ArrayList<> ();
        for (Menu menu : aa) {
            if (menu.getParentId() == null) {
                fu.add(menu);
            }
        }
        for (Menu fuMenu : fu) {
            List<Menu> ziMenu = new ArrayList<>();
            for (Menu zi : aa) {
                if (zi.getParentId() != null && zi.getParentId().equals(fuMenu.getId())) {
                    ziMenu.add(zi);
                }
            }
            fuMenu.setChildren(ziMenu);
        }
        return fu;
    }

    @Override
    public ResponseResult getassigndata(Integer roleId, Integer[] menuIds) {
        //删除
        int i = rm.delmenudata(roleId);
      //生成
        if (menuIds==null||menuIds.length==0) {

            return ResponseResult.getSuccess ();
        }else {
            int aa = rm.addmenudata(roleId,menuIds);
            return ResponseResult.getSuccess ();
        }
    }
}
